package com.foreknow.demo12;

import com.foreknow.demo2.A;
import com.foreknow.demo2.Admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        //1. 创建一个集合容器List
//        List list = new ArrayList();
//        List<Integer> list = new ArrayList<Integer>();
        List<Admin> list = new ArrayList<Admin>();
        //2. 向集合容器中添加元素  集合中只能存对象（object）
//        list.add(1);
//        list.add(3);
//        list.add(5);

        Admin admin = new Admin();
        admin.setName("tom1");
        Admin admin2 = new Admin();
        admin2.setName("tom2");
        Admin admin3 = new Admin();
        admin3.setName("tom3");

        list.add(admin);
        list.add(admin2);
        list.add(admin3);

        //3. 从集合当中获取某个元素
        Admin d = (Admin) list.get(0);
//        Integer d = (Integer) list.get(0);
        System.out.println(d);
        System.out.println("------------------");

        //4.遍历集合获取所有的元素
        for (int i = 0; i < list.size(); i++) {
//            Integer ii = list.get(i);
            Admin ii = list.get(i);
            System.out.println(ii.getName());
        }
        //5.也可以用增强的for循环
        for (Admin admin1:list) {
            System.out.println(admin1.getName());
        }
        //6.使用迭代器遍历list集合
        Iterator<Admin> it = list.iterator();
        while (it.hasNext()){
            Admin admin1 = it.next();
            System.out.println(admin1.getName());
        }
    }
}
